package com.hou.controller;

import com.hou.pojo.Vip;
import com.hou.service.ISeckillGoodsService;
import com.hou.service.deleteService;
import com.hou.vo.CaseVip;
import com.hou.vo.GoodsVo;
import com.hou.vo.JsonResult;
import com.hou.vo.TaomiGoods;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auoter:jackh 这一个类写有关秒杀商品后台的操作
 */
@RestController
@Controller
@RequestMapping("/hou")
public class houtai extends BaseController{
    @Autowired
    deleteService deleteService;
    @Autowired
    ISeckillGoodsService iSeckillGoodsService;

    /**
     * 这个控制器执行完需要重定向回当前页面
     *
     * @param id
     * @return
     */

    @RequestMapping(value = "/sekillg/delete" ,method = RequestMethod.DELETE)
    public RespBean delete(@CaseVip Vip vip, Long id) {
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        boolean b = deleteService.removeById(id);
        boolean b1 = iSeckillGoodsService.removeById(id);
        if (!b) {
            return RespBean.error(RespBeanEnum.DELETE_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/sekillg/select")
    public RespBean selectList(@CaseVip Vip vip) {
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<GoodsVo> goodsVos = deleteService.selectList();
        if (goodsVos==null) {
            return RespBean.error(RespBeanEnum.NULL_LIST_ERROR);
        }
        return RespBean.success(goodsVos);
    }

    /**
     * 添加秒杀商品
     * @param goodsVo
     * @return
     */
    @RequestMapping("/sekillg/add")
    public RespBean add(@CaseVip Vip vip,GoodsVo goodsVo){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        boolean b = deleteService.save1(goodsVo);

        if (!b){
            return RespBean.error(RespBeanEnum.ADD_ERROR);

        }
        return RespBean.success();
    }

    /**
     * 模糊查询产品接口，根据产品名称查询
     * @param name
     * @return
     */
    @RequestMapping("/sekillg/search")
    public RespBean select(@CaseVip Vip vip,String name){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<GoodsVo> select = deleteService.select(name);
        if (select==null){
            return RespBean.error(RespBeanEnum.NULL_GOODS_LIST);
        }
        return RespBean.success(select);
    }

    @RequestMapping("/sekillg/update")
    public RespBean updata(@CaseVip Vip vip,GoodsVo goodsVo){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        deleteService.updata(goodsVo);
        return RespBean.success();
    }

    @RequestMapping("/sekillg/selectid")
    public RespBean selectid(@CaseVip Vip vip,Long id) {
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        GoodsVo goodsVo=deleteService.selectid(id);
        if (goodsVo==null) {
            return RespBean.error(RespBeanEnum.SELECT_Error);
        }
        return RespBean.success(goodsVo);
    }

}
